package com.fz.us.admin.base.utils;

import java.io.*;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import ma.glasnost.orika.impl.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * Created by 9joint on 2017/7/18.
 */
public class ItextPdfUtil {



    public void  AddContentToPDF() throws IOException, DocumentException {
        //创建一个pdf读入流
        PdfReader reader = new PdfReader("d:/help.pdf");
        //根据一个pdfreader创建一个pdfStamper.用来生成新的pdf.
        PdfStamper stamper = new PdfStamper(reader,
                new FileOutputStream("d:/3.pdf"));

        //这个字体是itext-asian.jar中自带的 所以不用考虑操作系统环境问题.
    /*    BaseFont bf = BaseFont.createFont("STSong-Light",
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // set font*/
        BaseFont bf = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //baseFont不支持字体样式设定.但是font字体要求操作系统支持此字体会带来移植问题.
        Font font = new Font(bf,26);
        font.setStyle(Font.BOLD);
        font.getBaseFont();
        //页数是从1开始的
       for (int i=1; i<=5; i++){

            //获得pdfstamper在当前页的上层打印内容.也就是说 这些内容会覆盖在原先的pdf内容之上.
            PdfContentByte over = stamper.getOverContent(1);
            //用pdfreader获得当前页字典对象.包含了该页的一些数据.比如该页的坐标轴信息.
            PdfDictionary p = reader.getPageN(1);
            //拿到mediaBox 里面放着该页pdf的大小信息.
            PdfObject po =  p.get(new PdfName("MediaBox"));
            System.out.println(po.isArray());
            //po是一个数组对象.里面包含了该页pdf的坐标轴范围.
            PdfArray pa = (PdfArray) po;
            System.out.println(pa.size());
            //看看y轴的最大值.
            System.out.println(pa.getAsNumber(pa.size()-1));
            //开始写入文本
            over.beginText();
            //设置字体和大小
            over.setFontAndSize(font.getBaseFont(), 26);
            //设置字体颜色
            over.setColorFill(BaseColor.BLACK);
            //设置字体的输出位置
            over.setTextMatrix(250, 800);
            //要输出的text
            over.showText("中");
           // over.endText();

         //   over.setTextMatrix(200, 300);
            //要输出的text
        //   over.showText("国");
            over.endText();
            /*
            //创建一个image对象.
            Image image = Image.getInstance("d:/ca757c380cd79123c74a5691a6345982b0b780a6.jpg");
            //设置image对象的输出位置pa.getAsNumber(pa.size()-1).floatValue() 是该页pdf坐标轴的y轴的最大值
            image.setAbsolutePosition(0,pa.getAsNumber(pa.size()-1).floatValue()-100);//0, 0, 841.92, 595.32
            over.addImage(image);

            //画一个圈.
            over.setRGBColorStroke(0xFF, 0x00, 0x00);
            over.setLineWidth(5f);
            over.ellipse(250, 450, 350, 550);
            over.stroke();
            */
       }

        stamper.close();
    }
    public void test1() throws IOException, DocumentException {
        Document document = new Document();
        BaseFont bf = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //baseFont不支持字体样式设定.但是font字体要求操作系统支持此字体会带来移植问题.
        Font font = new Font(bf,26);
        font.setStyle(Font.BOLD);
        font.getBaseFont();
        BaseFont bf2 = BaseFont.createFont("c://windows//fonts//SIMFANG.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font font2 = new Font(bf2,8);
        font2.getBaseFont();
        Font font3 = new Font(bf2,14);
        font3.setStyle(Font.BOLD);
        font3.getBaseFont();
        try
        {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("1.pdf"));
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            cb.beginText();
            cb.setFontAndSize(bf2, 8);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "1-1121306-中", 297, 820, 0);
           /* cb.setFontAndSize(bf,26);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "中", 295, 200, 0);*/
            cb.endText();
            Paragraph t1 = new Paragraph(" ", font);
            Paragraph t2 = new Paragraph("中", font);
            Paragraph t3 = new Paragraph("国", font);
            Paragraph t4 = new Paragraph("药", font);
            Paragraph t5 = new Paragraph("事", font);
            Paragraph t6 = new Paragraph("卷 30", font3);
            Paragraph t7 = new Paragraph("7-9", font3);
            Paragraph t8 = new Paragraph("2016", font3);
            Paragraph t9 = new Paragraph("15638", font3);
            Paragraph t10 = new Paragraph("上海图书馆", font3);
            t1.setAlignment(1);
            t2.setAlignment(1);
            t3.setAlignment(1);
            t4.setAlignment(1);
            t5.setAlignment(1);
            t6.setAlignment(1);
            t7.setAlignment(1);
            t8.setAlignment(1);
            t9.setAlignment(1);
            t10.setAlignment(1);
            t1.setSpacingAfter(108f);
            t2.setSpacingBefore(10f);
            t3.setSpacingBefore(10f);
            t4.setSpacingBefore(10f);
            t5.setSpacingBefore(10f);
            t6.setSpacingBefore(110f);
            t9.setSpacingBefore(85f);
            document.add(t1);
            document.add(t2);
            document.add(t3);
            document.add(t4);
            document.add(t5);
            document.add(t6);
            document.add(t7);
            document.add(t8);
            document.add(t9);
            document.add(t10);
         /*   //Set attributes here
            document.addAuthor("Lokesh Gupta");
            document.addCreationDate();
            document.addCreator("HowToDoInJava.com");
            document.addTitle("Set Attribute Example");
            document.addSubject("An example to show how attributes can be added to pdf files.");*/
            document.newPage();
            document.add(new Paragraph("New page"));
            document.close();
            writer.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    public void doPdf() throws IOException, DocumentException {
        //pdf
        Document document = new Document();
        BaseFont bf = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //baseFont不支持字体样式设定.但是font字体要求操作系统支持此字体会带来移植问题.
        Font font = new Font(bf,26);
        font.setStyle(Font.BOLD);
        font.getBaseFont();
        BaseFont bf2 = BaseFont.createFont("c://windows//fonts//SIMFANG.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font font2 = new Font(bf2,8);
        font2.getBaseFont();
        Font font3 = new Font(bf2,14);
        font3.setStyle(Font.BOLD);
        font3.getBaseFont();
        BaseFont bf3 = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font font4 = new Font(bf3,14);
        font4.setStyle(Font.NORMAL);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("1.pdf"));
        document.open();
        //excel
        File file = new File("D:/原件test.xls");
        InputStream inputStream = new FileInputStream(file);
        Workbook workbook = new HSSFWorkbook(inputStream);
        workbook.setActiveSheet(0);
        Sheet sheet = workbook.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();
        for(int i=1; i<=rowNum; i++){
            HSSFRow row = (HSSFRow) sheet.getRow(i);
            String bindNo = row.getCell(0).getStringCellValue();
            String title = row.getCell(1).getStringCellValue();
            String no  = row.getCell(5).getStringCellValue();
            String qi = row.getCell(6).getStringCellValue();
            String zhong = row.getCell(7).getStringCellValue();
            String suoqu = row.getCell(2).getStringCellValue();
            String year = row.getCell(3).getStringCellValue();
            String version = row.getCell(11).getStringCellValue();
            if(i>1){
                document.newPage();
            }
            PdfContentByte cb = writer.getDirectContent();
            cb.beginText();
            cb.setFontAndSize(bf2, 8);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, i+"-"+bindNo+"-中", 297, 820, 0);
            cb.endText();
            Paragraph t1 = new Paragraph(" ", font);
            t1.setAlignment(1);
            t1.setSpacingAfter(108-(title.length()-4)*13);
            document.add(t1);
            for(int j=0;j<title.length();j++){
                Paragraph t2 = new Paragraph(String.valueOf(title.charAt(j)), font);
                t2.setAlignment(1);
                t2.setSpacingBefore(10f);
                document.add(t2);
            }
            if(StringUtils.isNotEmpty(version)){
                Paragraph t9 = new Paragraph(version, font4);
                t9.setAlignment(1);
                t9.setSpacingBefore(10f);
                document.add(t9);
            }

            Paragraph t3 = new Paragraph("卷 "+no, font3);
            t3.setSpacingBefore(110-(title.length()-4)*13- (StringUtils.isNotEmpty(version)?15:0) );
            Paragraph t4 = new Paragraph(qi, font3);
            Paragraph t5 = new Paragraph(year, font3);
            Paragraph t6 = new Paragraph("总 "+zhong, font3);
            Paragraph t7 = new Paragraph(suoqu, font3);
            t7.setSpacingBefore(85-(title.length()-4)*10);
            Paragraph t8 = new Paragraph("上海图书馆", font3);
            t3.setAlignment(1);
            t4.setAlignment(1);
            t5.setAlignment(1);
            t6.setAlignment(1);
            t7.setAlignment(1);
            t8.setAlignment(1);
            document.add(t3);
            document.add(t4);
            document.add(t5);
            document.add(t6);
            document.add(t7);
            document.add(t8);
        }
        document.close();
        writer.close();
    }
    public static void main(String s[])
    {
        ItextPdfUtil d=new ItextPdfUtil();
        try {
            d.doPdf();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }


    }
}
